package com.app.zenmewan.commons;

import com.app.zenmewan.commons.Message;
import com.app.zenmewan.entity.User;
import com.app.zenmewan.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;

import java.util.List;
@Component
public class Actions {
    @Resource
    private UserService userService;
    //token校验
    public int checkToken(String token, ModelAndView modelAndView) {
        if (token == null || token.isEmpty()) {
            modelAndView.setStatus(HttpStatus.BAD_REQUEST);
            modelAndView.addObject(Message.MESS, "TOKENNULL");
            return -1;
        } else {
            User u = new User();
            u.setToken(token);
            List<User> list = userService.queryAll(u);
            if (list.size() > 0) {
                return list.get(0).getId();
            } else {
                return -1;
            }
        }
    }
    //
    public int checkUid(String token){
        if (token == null || token.isEmpty()) {
            return -1;
        } else {
            User u = new User();
            u.setToken(token);
            List<User> list = userService.queryAll(u);
            if (list.size() > 0) {
                return list.get(0).getId();
            } else {
                return -1;
            }
        }
    }


}
